<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.initChart(val)
      }
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(val) {
      if (this.chart) {
        this.chart.dispose()
      }
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(val)
    },
    setOptions({ incomeData, disburseData, differenceData, xAxisData, meaning, selectedDate } = {}) {
      this.chart.setOption({
        animation: true,
        animationDuration: 1500,
        xAxis: {
          data: xAxisData,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLabel: {
            interval: function (index, value) {
              return index % 2 === 0
            }
          }
        },
        grid: {
          left: 10,
          right: 20,
          bottom: 20,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          name: '单位：元',
          // min: 0, // 设置Y轴最小值
          // max: 300, // 设置Y轴最大值
          // interval: 20, // 设置Y轴刻度间隔
          axisTick: {
            show: false
          }
        },
        legend: {
          data: meaning,
          selected: selectedDate
        },
        series: [
          {
            name: meaning[0],
            lineStyle: {
              color: '#FF8871'
            },
            itemStyle: {
              normal: {
                color: '#FF8871'
              }
            },
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#FF8871'
                },
                {
                  offset: 1,
                  color: '#fec9bf'
                }
              ])
            },
            label: {
              show: true,
              position: 'top',
              formatter: function(params) {
                return params.value > 0 && params.dataIndex % 2 !== 0 ? params.value : ''
              }
            }, // 显示数值
            // step: 'start', // 连接类型
            smooth: true,
            type: 'line',
            data: incomeData,
            animationEasing: 'cubicInOut'
          },
          {
            name: meaning[1],
            smooth: true,
            type: 'line',
            lineStyle: {
              color: '#3888fa'
            },
            itemStyle: {
              normal: {
                color: '#3888fa'
              }
            },
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#3888fa'
                },
                {
                  offset: 1,
                  color: '#c8ddfb'
                }
              ])
            },
            label: {
              show: true,
              position: 'top',
              formatter: function(params) {
                return params.value > 0 && params.dataIndex % 2 === 0 ? params.value : ''
              }
            }, // 显示数值
            data: disburseData,
            animationEasing: 'quadraticOut'
          },
          {
            name: meaning[2],
            smooth: true,
            type: 'line',
            lineStyle: {
              color: '#e676d2'
            },
            itemStyle: {
              normal: {
                color: '#e676d2'
              }
            },
            areaStyle: {
              opacity: 0.8,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: '#e676d2'
                },
                {
                  offset: 1,
                  color: '#c8ddfb'
                }
              ])
            },
            label: {
              show: true,
              position: 'top',
              formatter: function(params) {
                return params.value > 0 ? params.value : ''
              }
            }, // 显示数值
            data: differenceData,
            animationEasing: 'quadraticOut'
          }
        ]
      })
    }
  }
}
</script>
